package br.eng.eliseu.gwt.aconchego.camera.module;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import com.google.gwt.user.client.rpc.IsSerializable;


@Entity
@Table(name="ACESSOCAM")
@SequenceGenerator(name="SEQ_ACESSOCAM", sequenceName="SEQ_ACESSOCAM")
@NamedQueries({
	@NamedQuery(name="acessoCam.selectTudo", query="select q from AcessoCam q "),
	@NamedQuery(name="acessoCam.selectAcessoUsuario", query="select q from AcessoCam q where q.usuario = :usuario " + 
	"and q.status = :statusUsuario "),
})
public class AcessoCam implements Serializable, IsSerializable {
	
	private static final long serialVersionUID = -6166122062937119048L;


	public AcessoCam() {}
	

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
	
	@OneToOne( fetch = FetchType.EAGER,cascade=CascadeType.ALL )
	@JoinColumn(name="usuario_id")
	private Usuario usuario;

	@Enumerated(EnumType.STRING)
	private StatusEnum status;
	
	@Enumerated(EnumType.STRING)
	private GrupoEnum grupo;
	
	@Temporal(TemporalType.TIME)
	private Date periodoInicio;
	
	@Temporal(TemporalType.TIME)
	private Date periodoFim;


	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(	name="acessoCam_camera", 
    			joinColumns = {@JoinColumn(name = "acessoCam_id", referencedColumnName="id", unique=false)},
    			inverseJoinColumns ={@JoinColumn(name = "camera_id",referencedColumnName="id",unique=false)},
    			uniqueConstraints={@UniqueConstraint(name="acessoCam_camera_ids", columnNames={"acessoCam_id","camera_id"})} 
	) 
	private List<Camera> cameras;
	
	
	
	/**
	 * Pega o list que foi populado pelo hibernate e repopula manualmente cada item.
	 * Esta acao faz com que o list seja serializado pelo RPC 
	 */
	public void repopulaListCameras(){
		List<Camera> c = new ArrayList<Camera>();
		for (Camera camera : getCameras()) {
			c.add(camera);
		}
		setCameras(c);
	}

	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public Usuario getUsuario() {
		usuario = (usuario==null)?new Usuario():usuario;
		return usuario;
	}
	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public StatusEnum getStatus() {
		return status;
	}
	public void setStatus(StatusEnum status) {
		this.status = status;
	}

	public GrupoEnum getGrupo() {
		return grupo;
	}
	public void setGrupo(GrupoEnum grupo) {
		this.grupo = grupo;
	}

	public Date getPeriodoInicio() {
		return periodoInicio;
	}
	public void setPeriodoInicio(Date periodoInicio) {
		this.periodoInicio = periodoInicio;
	}

	public Date getPeriodoFim() {
		return periodoFim;
	}
	public void setPeriodoFim(Date periodoFim) {
		this.periodoFim = periodoFim;
	}

	public List<Camera> getCameras() {
		return cameras;
	}
	public void setCameras(List<Camera> cameras) {
		this.cameras = cameras;
	}

}
